package cn.edu.java.io.outputstream;

import lombok.SneakyThrows;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayOutputStreamDemo {
    @SneakyThrows
    public static void main(String[] args) {
        String data = "Hello, World!";
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        // 将字符串数据写入ByteArrayOutputStream
        byteArrayOutputStream.write(data.getBytes());

        // 获取写入的字节数组
        byte[] byteArray = byteArrayOutputStream.toByteArray();

        // 打印写入的数据
        System.out.println("ByteArrayOutputStream 写入的数据为: " + new String(byteArray));

        // 关闭数据流
        byteArrayOutputStream.close();

    }
}

